﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM.UserInterface
{
    public partial class cbWard : UserControl
    {
        public string Dist_ID;
        public string Ward_ID;
        public string Ward_Name;
        //──────────────────────────────────────────────────────────────────
        public cbWard()
        {
            InitializeComponent();
            //this.cbWardName.isWard = true;
            this.Load += new EventHandler(cbWard_Load);
            this.cbWardName.SelectedValueChanged+=new EventHandler(cbWardName_SelectedIndexChanged);
            this.cbWardName.TextChanged += new EventHandler(cbWardName_TextChanged);
        }

        void cbWardName_TextChanged(object sender, EventArgs e)
        {
            if(this.cbWardName.Text=="")
                    cbWardName_SelectedIndexChanged(sender, e);
        }
        //──────────────────────────────────────────────────────────────────
        void cbWard_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    if (this.cbWardName.DataSource == null)
            //    {
            //        DataTable dt = null;
            //        if (Dist_ID != null)
            //            dt = new CMS_GYM.Address().dt_getWard(Dist_ID);
            //        else
            //            dt = new CMS_GYM.Address().dt_getWard();
            //        if (dt == null) return;
            //        this.cbWardName.DataSource = new DataView(dt);
            //        this.cbWardName.DisplayMember = dt.Columns[2].ColumnName;
            //        this.cbWardName.ValueMember = dt.Columns[0].ColumnName;

            //        cbWardName_SelectedIndexChanged(sender, e);
            //        //cbWardName.isWard = true;
            //    }
            //}
            //catch (Exception) { }
        }

        public void LoadData()
        {
            try
            {
                if (this.cbWardName.DataSource == null)
                {
                    DataTable dt = null;
                    if (Dist_ID != null)
                        dt = new CMS_GYM.Address().dt_getWard(Dist_ID);
                    else
                        dt = new CMS_GYM.Address().dt_getWard();
                    if (dt == null) return;
                    this.cbWardName.DataSource = new DataView(dt);
                    this.cbWardName.DisplayMember = dt.Columns[2].ColumnName;
                    this.cbWardName.ValueMember = dt.Columns[0].ColumnName;

                    cbWardName_SelectedIndexChanged(null, null);
                    //cbWardName.isWard = true;
                }
            }
            catch (Exception) { }
        }
        //──────────────────────────────────────────────────────────────────
        void cbWardName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged();
        }
        //──────────────────────────────────────────────────────────────────
        private void SelectedIndexChanged()
        {
            if (cbWardName.SelectedValue == null)
            {
                Ward_ID = "";
                Ward_Name = "";
                return;
            }
            Ward_ID = cbWardName.SelectedValue == null ? string.Empty : cbWardName.SelectedValue.ToString();
            Ward_Name = this.cbWardName.Text;
        }
        //──────────────────────────────────────────────────────────────────
        public void SetWard(string s_WardID)
        {
            if (s_WardID == "" || s_WardID == null || s_WardID == "NULL")
            {
                this.cbWardName.SelectedValue = DBNull.Value;
                this.cbWardName.Text = "";
            }
            else
            {
                this.cbWardName.SelectedValue = s_WardID;

            }
        }
        //──────────────────────────────────────────────────────────────────
        public void setDataSource(DataTable dtWard)
        {
            if (dtWard == null) return;
            this.cbWardName.DataSource = dtWard;
            this.cbWardName.DisplayMember = dtWard.Columns[2].ColumnName;
            this.cbWardName.ValueMember = dtWard.Columns[0].ColumnName;
            
            
                 SelectedIndexChanged();
                 if (dtWard.Rows.Count == 0)
                     this.cbWardName.SelectedValue=string.Empty;
        }
        //──────────────────────────────────────────────────────────────────
        public void setDistrict(string sDistID,string LINK_SERVER)
        {
            //DataTable dt = Address.dt_getWard(sDistID);
            DataTable dt = new CMS_GYM.Ward().dt_getWards(sDistID);
            this.setDataSource(dt);
        }
        public void LoadDanhMucPhuong(string LINK_SERVER)
        {
            DataTable dt = new CMS_GYM.Ward().LayDanhMucPhuong();
            this.setDataSource(dt);
        }
        public void setDistrict(string sDistID)
        {
            DataTable dt = new CMS_GYM.Address().dt_getWard(sDistID);           
            this.setDataSource(dt);
        }
        //──────────────────────────────────────────────────────────────────      
    }
}
